package com.tuyrt.as.proj.main.logic;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentManager;

import com.tuyrt.as.proj.common.tab.HiFragmentTabView;
import com.tuyrt.as.proj.common.tab.HiTabViewAdapter;
import com.tuyrt.as.proj.main.R;
import com.tuyrt.as.proj.main.fragment.category.CategoryFragment;
import com.tuyrt.as.proj.main.fragment.FavoriteFragment;
import com.tuyrt.as.proj.main.fragment.home.HomePageFragment;
import com.tuyrt.as.proj.main.fragment.profile.ProfileFragment;
import com.tuyrt.as.proj.main.fragment.RecommendFragment;
import com.tuyrt.hi.ui.tab.bottom.HiTabBottomInfo;
import com.tuyrt.hi.ui.tab.bottom.HiTabBottomLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator on 2021/4/29.
 * desc: 将MainActivity的一些逻辑内聚在此，让MainActivity逻辑更清晰
 */
public class MainActivityLogic {

    private HiTabBottomLayout hiTabBottomLayout;
    private HiFragmentTabView fragmentTabView;
    private List<HiTabBottomInfo<?>> infoList;
    private int currentItemIndex;
    private ActivityProvider activityProvider;
    private final static String SAVED_CURRENT_ID = "SAVED_CURRENT_ID";

    public MainActivityLogic(ActivityProvider activityProvider, @Nullable Bundle savedInstanceState) {
        this.activityProvider = activityProvider;
        // fix: 不保留活动导致的Fragment重叠问题
        if (savedInstanceState != null) {
            currentItemIndex = savedInstanceState.getInt(SAVED_CURRENT_ID);
        }
        initTabBottom();
    }

    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putInt(SAVED_CURRENT_ID, currentItemIndex);
    }

    private void initTabBottom() {
        hiTabBottomLayout = activityProvider.findViewById(R.id.hi_tab_layout);
        hiTabBottomLayout.setTabAlpha(0.85f);
        int defaultColor = activityProvider.getResources().getColor(R.color.tabBottomDefaultColor);
        int tintColor = activityProvider.getResources().getColor(R.color.tabBottomTintColor);

        infoList = new ArrayList();
        HiTabBottomInfo hiHome = new HiTabBottomInfo<Integer>(
                "首页",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_home),
                null,
                defaultColor,
                tintColor
        );
        hiHome.fragment = HomePageFragment.class;
        HiTabBottomInfo hiFavorite = new HiTabBottomInfo<Integer>(
                "收藏",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_recommend),
                null,
                defaultColor,
                tintColor
        );
        hiFavorite.fragment = FavoriteFragment.class;
        HiTabBottomInfo hiCategory = new HiTabBottomInfo<Integer>(
                "分类",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_favorite1),
                null,
                defaultColor,
                tintColor
        );
        hiCategory.fragment = CategoryFragment.class;
        HiTabBottomInfo hiRecommend = new HiTabBottomInfo<Integer>(
                "推荐",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_chat),
                null,
                defaultColor,
                tintColor
        );
        hiRecommend.fragment = RecommendFragment.class;
        HiTabBottomInfo hiProfile = new HiTabBottomInfo<Integer>(
                "我的",
                "fonts/iconfont.ttf",
                activityProvider.getString(R.string.if_profile),
                null,
                defaultColor,
                tintColor
        );
        hiProfile.fragment = ProfileFragment.class;
        infoList.add(hiHome);
        infoList.add(hiFavorite);
        infoList.add(hiCategory);
        infoList.add(hiRecommend);
        infoList.add(hiProfile);
        hiTabBottomLayout.inflateInfo(infoList);
        // 在此初始化FragmentTabView, hiTabBottomLayout监听变化时，会调用 fragmentTabView
        initFragmentTabView(infoList);
        hiTabBottomLayout.addOnTabSelectedChangeListener((index, preInfo, nextInfo) -> {
            fragmentTabView.setCurrentItem(index);
            currentItemIndex = index;
        });
        hiTabBottomLayout.defaultSelected(infoList.get(currentItemIndex));
    }

    private void initFragmentTabView(List<HiTabBottomInfo<?>> infoList) {
        fragmentTabView = activityProvider.findViewById(R.id.fragment_tab_view);
        FragmentManager fragmentManager = activityProvider.getSupportFragmentManager();
        HiTabViewAdapter adapter = new HiTabViewAdapter(fragmentManager, infoList);
        fragmentTabView.setAdapter(adapter);
    }

    public HiTabBottomLayout getHiTabBottomLayout() {
        return hiTabBottomLayout;
    }

    public HiFragmentTabView getFragmentTabView() {
        return fragmentTabView;
    }

    public List<HiTabBottomInfo<?>> getInfoList() {
        return infoList;
    }

    /**
     * 此类中需要使用Activity中提供的能力
     * Tips:这些方法和AppCompatActivity中的父类的方法名称高度一致，避免在实现此接口的Activity中覆写
     */
    public interface ActivityProvider {

        <T extends View> T findViewById(@IdRes int id);

        Resources getResources();

        String getString(@StringRes int resId);

        FragmentManager getSupportFragmentManager();
    }
}
